package org.iWMS.core.service;


import org.apache.log4j.Logger;
import org.iWMS.core.dao.LoginLogDao;
import org.iWMS.core.dao.UserDao;
import org.iWMS.core.domain.LoginLog;
import org.iWMS.core.domain.User;
import org.iWMS.core.log.LogRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 * User: issuser
 * Date: 13-5-2
 * Time: 下午3:28
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserService {
    private final static Logger logger=Logger.getLogger(UserService.class);
    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hasMatchUser(String userName, String password) {
        int matchCount =userDao.getMatchCount(userName, password);
        return matchCount > 0;
    }

    @LogRecord(description="根据用户名查询用户信息")
    public User findUserByUserName(String userName) {

        return userDao.findUserByUserName(userName);
    }


    @Transactional
    public void loginSuccess(User user) {
        LoginLog loginLog = new LoginLog();
        loginLog.setUserId(user.getUserId());
        loginLog.setIp(user.getLastIp());
        loginLog.setLoginDate(user.getLastVisit());
        user.setRoleList(userDao.findRoleByUser(user));
        userDao.updateLoginInfo(user);
        loginLogDao.insertLoginLog(loginLog);
    }

    public void updateInfo(User user) {
        userDao.updateUserById(user);
    }
}

